package com.cqxyd.smartsite.aop;

import java.util.Arrays;
import java.util.List;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import com.github.pagehelper.PageHelper;

@Aspect
@Component
public class PagListAspect {

    @Before("execution(public * com.cqxyd.smartsite.service.*.*.get*ListByPage(..))")
    public void doBeforeGetList(JoinPoint joinPoint) {
        // 获取分页的参数信息
        MethodSignature msg = (MethodSignature) joinPoint.getSignature();
        String[] paramName = msg.getParameterNames();
        List<String> paramNameList = Arrays.asList(paramName);
        Object[] args = joinPoint.getArgs();
        if (args.length >= 2 && paramNameList.contains("pageNum") && paramNameList.contains("pageSize")) {
            PageHelper.startPage(Integer.valueOf(args[paramNameList.indexOf("pageNum")].toString()),
                    Integer.valueOf(args[paramNameList.indexOf("pageSize")].toString()));// 写在查询之前
        }
    }

}
